﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis.Support;

namespace SpiderServer
{
	public class MessageQueue<T> where T : class
	{
		private string name;
		private ObjectSerializer serializer = new ObjectSerializer();
		public MessageQueue(string name)
		{
			this.name = name;
		}

		public T Pop()
		{
			using (var redisClent = Global.GetRedisClient())
			{
				byte[] bytes = redisClent.LPop(name);
				if (bytes == null)
				{
					return null;
				}
				return (T)serializer.Deserialize(bytes);
			}
		}

		public void Push(T item)
		{
			using (var redisClent = Global.GetRedisClient())
			{
				redisClent.LPush(name, serializer.Serialize(item));
			}
		}

		public void RPush(T item)
		{
			using (var redisClent = Global.GetRedisClient())
			{
				redisClent.RPush(name, serializer.Serialize(item));
			}
		}
	}
}
